package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;

import com.api.shopapi.entity.dto.NursingSchedulingDTO;
import com.api.shopapi.entity.dto.NursingSchedulingDTO1;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author maochuan
 */
@RestController
@RequestMapping("NursingScheduling")
public class NursingSchedulingController extends BaseController {

    @Autowired
    @Qualifier("NursingSchedulingServiceImplOne")
    private NursingSchedulingService nursingSchedulingService;

    @Autowired
    @Qualifier("NursingSchedulingDaytimeServiceImplOne")
    private NursingSchedulingDaytimeService nursingSchedulingDaytimeService;

    @Autowired
    @Qualifier("NursingSchedulingDaytimeItemServiceImplOne")
    private NursingSchedulingDaytimeItemService nursingSchedulingDaytimeItemService;

    @Autowired
    @Qualifier("NursingSchedulingNighttimeServiceImplOne")
    private NursingSchedulingNighttimeService nursingSchedulingNighttimeService;

    @Autowired
    @Qualifier("NursingSchedulingNighttimeItemServiceImplOne")
    private NursingSchedulingNighttimeItemService nursingSchedulingNighttimeItemService;

    @Autowired
    @Qualifier("NursingSchedulingMorningServiceImplOne")
    private NursingSchedulingMorningService nursingSchedulingMorningService;

    @Autowired
    @Qualifier("NursingSchedulingMorningItemServiceImplOne")
    private NursingSchedulingMorningItemService nursingSchedulingMorningItemService;

    @Autowired
    @Qualifier("NursingSchedulingNoonServiceImplOne")
    private NursingSchedulingNoonService nursingSchedulingNoonService;

    @Autowired
    @Qualifier("NursingSchedulingNoonItemServiceImplOne")
    private NursingSchedulingNoonItemService nursingSchedulingNoonItemService;

    @Autowired
    @Qualifier("NursingSchedulingNightServiceImplOne")
    private NursingSchedulingNightService nursingSchedulingNightService;

    @Autowired
    @Qualifier("NursingSchedulingNightItemServiceImplOne")
    private NursingSchedulingNightItemService nursingSchedulingNightItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("NurseAideServiceImplOne")
    private NurseAideService nurseAideService;

    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;

    @Autowired
    @Qualifier("CheckinSetmealServiceImplOne")
    private CheckinSetmealService checkinSetmealService;

    @Autowired
    @Qualifier("BerthServiceImplOne")
    private BerthService berthService;

    @Autowired
    @Qualifier("RoomServiceImplOne")
    private RoomService roomService;

    @Autowired
    @Qualifier("BuildingServiceImplOne")
    private BuildingService buildingService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    @Autowired
    @Qualifier("CheckinServiceImplOne")
    private CheckinService checkinService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int type = jsonObject.getInteger("type");
            String time = jsonObject.getString("time");
            Date times = getTimeString(time);
            JSONArray jOSchedulingPlan = jsonObject.getJSONArray("schedulingPlan");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingScheduling/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            NursingScheduling nursingScheduling = nursingSchedulingService.getByTime(times,institutionCode);

            if (nursingScheduling == null) {
                //配餐计划插入
                nursingScheduling = new NursingScheduling();
                String nursingSchedulingCode = UUID.randomUUID().toString();
                nursingScheduling.setNursingSchedulingCode(nursingSchedulingCode);
                nursingScheduling.setType(type);
                nursingScheduling.setDay(getWeek(time));
                nursingScheduling.setTime(times);
                nursingScheduling.setCreateTime(new Date());
                nursingScheduling.setStaffCode(staffCode);
                nursingScheduling.setInstitutionCode(institutionCode);
                nursingSchedulingService.insert(nursingScheduling,jOSchedulingPlan,nursingSchedulingCode,type);
            } else {
                return failed("此日期排班已存在");
            }


            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/NursingScheduling/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 相同计划批量添加
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "AddMany", method = RequestMethod.POST)
    public MyResult AddMany(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingScheduling/AddMany");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<String> stringList = nursingSchedulingService.insert1(jsonObject);
            return success(stringList);
        } catch (Exception e) {
            insertError("/NursingScheduling/AddMany", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String nursingSchedulingCode = jsonObject.getString("nursingSchedulingCode");
            int type = jsonObject.getInteger("type");
            int status = jsonObject.getInteger("status");
            String time = jsonObject.getString("time");
            String remarks = jsonObject.getString("remarks");

            JSONArray jOSchedulingPlan = jsonObject.getJSONArray("schedulingPlan");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingScheduling/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date times = sdf.parse(time);
            String nowTime = sdf.format(new Date());
            if(times.getTime()<sdf.parse(nowTime).getTime()){
                return failed("不能修改之前日期的排班");
            }

            NursingScheduling nursingScheduling = nursingSchedulingService.getByTime(times,institutionCode);
            if(nursingScheduling!=null){
                if(!nursingScheduling.getNursingSchedulingCode().equals(nursingSchedulingCode)){
                   return failed("此日期排班已存在");
                }
            }

            nursingScheduling.setType(type);
            nursingScheduling.setStatus(status);
            nursingScheduling.setDay(getWeek(time));
            nursingScheduling.setTime(times);
            nursingSchedulingService.updateByNursingSchedulingCode(nursingScheduling,jOSchedulingPlan,type,remarks);


            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/NursingScheduling/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingScheduling/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<NursingScheduling> nursingSchedulingPageInfo = nursingSchedulingService.getPageList(pageIndex, pageSize,institutionCode, startTime, endTime);

            List<NursingSchedulingDTO> listNursingSchedulingDTO = null;
            if (nursingSchedulingPageInfo != null) {
                listNursingSchedulingDTO = new ArrayList<>();
                for (NursingScheduling nursingScheduling : nursingSchedulingPageInfo.getList()) {
                    NursingSchedulingDTO nursingSchedulingDTO = mapper.map(nursingScheduling, NursingSchedulingDTO.class);
                    listNursingSchedulingDTO.add(nursingSchedulingDTO);
                }

                for (NursingSchedulingDTO nursingSchedulingDTO : listNursingSchedulingDTO) {
                    int type = nursingSchedulingDTO.getType();
                    if(type==2){ //查询两班制
                        //早班
                        NursingSchedulingDaytime nursingSchedulingDaytime = nursingSchedulingDaytimeService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingDaytime!=null){
                            List<NursingSchedulingDaytimeItem> nursingSchedulingDaytimeItemList = nursingSchedulingDaytimeItemService.getByNursingSchedulingDayTimeCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode());
                            nursingSchedulingDaytime.setNursingSchedulingDaytimeItemList(nursingSchedulingDaytimeItemList);
                            nursingSchedulingDTO.setNursingSchedulingDaytime(nursingSchedulingDaytime);
                        }
                        //晚班
                        NursingSchedulingNighttime nursingSchedulingNighttime = nursingSchedulingNighttimeService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingNighttime!=null){
                            List<NursingSchedulingNighttimeItem> nursingSchedulingNighttimeItemList = nursingSchedulingNighttimeItemService.getByNursingSchedulingNightTimeCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode());
                            nursingSchedulingNighttime.setNursingSchedulingNighttimeItemList(nursingSchedulingNighttimeItemList);
                            nursingSchedulingDTO.setNursingSchedulingNighttime(nursingSchedulingNighttime);
                        }
                    }
                    if(type==3){
                        //早班
                        NursingSchedulingMorning nursingSchedulingMorning = nursingSchedulingMorningService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingMorning!=null){
                            List<NursingSchedulingMorningItem> nursingSchedulingMorningItemList = nursingSchedulingMorningItemService.getListByNursingSchedulingMorningCode(nursingSchedulingMorning.getNursingSchedulingMorningCode());
                            nursingSchedulingMorning.setNursingSchedulingMorningItemList(nursingSchedulingMorningItemList);
                            nursingSchedulingDTO.setNursingSchedulingMorning(nursingSchedulingMorning);
                        }
                        //中班
                        NursingSchedulingNoon nursingSchedulingNoon = nursingSchedulingNoonService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingNoon!=null){
                            List<NursingSchedulingNoonItem> nursingSchedulingNoonItemList = nursingSchedulingNoonItemService.getListByNursingSchedulingNoonCode(nursingSchedulingNoon.getNursingSchedulingNoonCode());
                            nursingSchedulingNoon.setNursingSchedulingNoonItemList(nursingSchedulingNoonItemList);
                            nursingSchedulingDTO.setNursingSchedulingNoon(nursingSchedulingNoon);
                        }
                        //晚班
                        NursingSchedulingNight nursingSchedulingNight = nursingSchedulingNightService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingNight!=null){
                            List<NursingSchedulingNightItem> nursingSchedulingNightItemList = nursingSchedulingNightItemService.getListByNursingSchedulingNightCode(nursingSchedulingNight.getNursingSchedulingNightCode());
                            nursingSchedulingNight.setNursingSchedulingNightItemList(nursingSchedulingNightItemList);
                            nursingSchedulingDTO.setNursingSchedulingNight(nursingSchedulingNight);
                        }
                    }
                }
                nursingSchedulingPageInfo.setList(Collections.unmodifiableList(listNursingSchedulingDTO));
            }

            return success(nursingSchedulingPageInfo);
        } catch (Exception e) {
            insertError("/NursingScheduling/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingScheduling/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<NursingScheduling> nursingSchedulingPageInfo = nursingSchedulingService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode(), startTime, endTime);

            List<NursingSchedulingDTO> listNursingSchedulingDTO = null;
            if (nursingSchedulingPageInfo != null) {
                listNursingSchedulingDTO = new ArrayList<>();
                for (NursingScheduling nursingScheduling : nursingSchedulingPageInfo.getList()) {
                    NursingSchedulingDTO nursingSchedulingDTO = mapper.map(nursingScheduling, NursingSchedulingDTO.class);
                    listNursingSchedulingDTO.add(nursingSchedulingDTO);
                }

                for (NursingSchedulingDTO nursingSchedulingDTO : listNursingSchedulingDTO) {
                    int type = nursingSchedulingDTO.getType();
                    if(type==2){ //查询两班制
                        //早班
                        NursingSchedulingDaytime nursingSchedulingDaytime = nursingSchedulingDaytimeService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingDaytime!=null){
                            List<NursingSchedulingDaytimeItem> nursingSchedulingDaytimeItemList = nursingSchedulingDaytimeItemService.getByNursingSchedulingDayTimeCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode());
                            nursingSchedulingDaytime.setNursingSchedulingDaytimeItemList(nursingSchedulingDaytimeItemList);
                            nursingSchedulingDTO.setNursingSchedulingDaytime(nursingSchedulingDaytime);
                        }
                        //晚班
                        NursingSchedulingNighttime nursingSchedulingNighttime = nursingSchedulingNighttimeService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingNighttime!=null){
                            List<NursingSchedulingNighttimeItem> nursingSchedulingNighttimeItemList = nursingSchedulingNighttimeItemService.getByNursingSchedulingNightTimeCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode());
                            nursingSchedulingNighttime.setNursingSchedulingNighttimeItemList(nursingSchedulingNighttimeItemList);
                            nursingSchedulingDTO.setNursingSchedulingNighttime(nursingSchedulingNighttime);
                        }
                    }
                    if(type==3){
                        //早班
                        NursingSchedulingMorning nursingSchedulingMorning = nursingSchedulingMorningService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingMorning!=null){
                            List<NursingSchedulingMorningItem> nursingSchedulingMorningItemList = nursingSchedulingMorningItemService.getListByNursingSchedulingMorningCode(nursingSchedulingMorning.getNursingSchedulingMorningCode());
                            nursingSchedulingMorning.setNursingSchedulingMorningItemList(nursingSchedulingMorningItemList);
                            nursingSchedulingDTO.setNursingSchedulingMorning(nursingSchedulingMorning);
                        }
                        //中班
                        NursingSchedulingNoon nursingSchedulingNoon = nursingSchedulingNoonService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingNoon!=null){
                            List<NursingSchedulingNoonItem> nursingSchedulingNoonItemList = nursingSchedulingNoonItemService.getListByNursingSchedulingNoonCode(nursingSchedulingNoon.getNursingSchedulingNoonCode());
                            nursingSchedulingNoon.setNursingSchedulingNoonItemList(nursingSchedulingNoonItemList);
                            nursingSchedulingDTO.setNursingSchedulingNoon(nursingSchedulingNoon);
                        }
                        //晚班
                        NursingSchedulingNight nursingSchedulingNight = nursingSchedulingNightService.getByNursingSchedulingCode(nursingSchedulingDTO.getNursingSchedulingCode());
                        if(nursingSchedulingNight!=null){
                            List<NursingSchedulingNightItem> nursingSchedulingNightItemList = nursingSchedulingNightItemService.getListByNursingSchedulingNightCode(nursingSchedulingNight.getNursingSchedulingNightCode());
                            nursingSchedulingNight.setNursingSchedulingNightItemList(nursingSchedulingNightItemList);
                            nursingSchedulingDTO.setNursingSchedulingNight(nursingSchedulingNight);
                        }
                    }
                }
                nursingSchedulingPageInfo.setList(Collections.unmodifiableList(listNursingSchedulingDTO));
            }

            return success(nursingSchedulingPageInfo);
        } catch (Exception e) {
            insertError("/NursingScheduling/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 查询自己的排班信息
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "GetNurseAideWorking", method = RequestMethod.POST)
    public MyResult GetNurseAideWorking(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/NursingScheduling/GetNurseAideWorking");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            User user = userService.getByUserCode(userCode);
            int userType = user.getUserType();
            String nurseCode ="";
            String nurseAideCode ="";
            String institutionCode = user.getInstitutionCode();
            if(userType==4){

            }else if(userType==5){
                NurseAide nurseAide = nurseAideService.getByUserCode(userCode);
                nurseAideCode = nurseAide.getNurseAideCode();
            }else {
                return failed("用户类型错误");
            }


            PageInfo<NursingScheduling> nursingSchedulingPageInfo = nursingSchedulingService.getPageList(pageIndex,pageSize,institutionCode,startTime,endTime);

            List<NursingSchedulingDTO1> nursingSchedulingDTO1List = new ArrayList<>();
            for (NursingScheduling nursingScheduling:nursingSchedulingPageInfo.getList()){
                NursingSchedulingDTO1 nursingSchedulingDTO1 = mapper.map(nursingScheduling,NursingSchedulingDTO1.class);
                nursingSchedulingDTO1List.add(nursingSchedulingDTO1);
            }
            for (NursingSchedulingDTO1 nursingSchedulingDTO1:nursingSchedulingDTO1List){
                int type = nursingSchedulingDTO1.getType();
                if(type==2) {
                    //早班
                    NursingSchedulingDaytime nursingSchedulingDaytime = nursingSchedulingDaytimeService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
                    if(nursingSchedulingDaytime!=null){
                        NursingSchedulingDaytimeItem nursingSchedulingDaytimeItem = null;
                        if(userType==4){
                            nursingSchedulingDaytimeItem = nursingSchedulingDaytimeItemService.getByNursingSchedulingDayTimeCodeAndNurseCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode(),nurseCode);
                        }else if(userType==5){
                            nursingSchedulingDaytimeItem = nursingSchedulingDaytimeItemService.getByNursingSchedulingDayTimeCodeAndNurseAideCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode(),nurseAideCode);
                        }

                        if(nursingSchedulingDaytimeItem!=null){
                            nursingSchedulingDTO1.setNursingSchedulingDaytime("早班");
                        }
                    }
                    //晚班
                    NursingSchedulingNighttime nursingSchedulingNighttime = nursingSchedulingNighttimeService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
                    if(nursingSchedulingNighttime!=null){
                        NursingSchedulingNighttimeItem nursingSchedulingNighttimeItem = null;
                        if(userType==4){
                            nursingSchedulingNighttimeItem = nursingSchedulingNighttimeItemService.getByNursingSchedulingNightTimeCodeAndNurseCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode(),nurseCode);
                        }else if(userType==5){
                            nursingSchedulingNighttimeItem = nursingSchedulingNighttimeItemService.getByNursingSchedulingNightTimeCodeAndNurseAideCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode(),nurseAideCode);
                        }
                        if(nursingSchedulingNighttimeItem!=null){
                            nursingSchedulingDTO1.setNursingSchedulingNighttime("晚班");
                        }
                    }
                }
                if(type==3){
                    //早班
                    NursingSchedulingMorning nursingSchedulingMorning = nursingSchedulingMorningService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
                    if(nursingSchedulingMorning!=null){
                        NursingSchedulingMorningItem nursingSchedulingMorningItem = null;
                        if(userType==4){
                            nursingSchedulingMorningItem = nursingSchedulingMorningItemService.getByNursingSchedulingMorningCodeAndNurseCode(nursingSchedulingMorning.getNursingSchedulingMorningCode(),nurseCode);
                        }else if(userType==5){
                            nursingSchedulingMorningItem = nursingSchedulingMorningItemService.getByNursingSchedulingMorningCodeAndNurseAideCode(nursingSchedulingMorning.getNursingSchedulingMorningCode(),nurseAideCode);
                        }
                        if(nursingSchedulingMorningItem!=null){
                            nursingSchedulingDTO1.setNursingSchedulingMorning("早班");
                        }
                    }
                    //中班
                    NursingSchedulingNoon nursingSchedulingNoon = nursingSchedulingNoonService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
                    if(nursingSchedulingNoon!=null){
                        NursingSchedulingNoonItem nursingSchedulingNoonItem = null;
                        if(userType==4){
                            nursingSchedulingNoonItem = nursingSchedulingNoonItemService.getByNursingSchedulingNoonCodeAndNurseCode(nursingSchedulingNoon.getNursingSchedulingNoonCode(),nurseCode);
                        }else if(userType==5){
                            nursingSchedulingNoonItem = nursingSchedulingNoonItemService.getByNursingSchedulingNoonCodeAndNurseAideCode(nursingSchedulingNoon.getNursingSchedulingNoonCode(),nurseAideCode);
                        }
                        if(nursingSchedulingNoonItem!=null){
                            nursingSchedulingDTO1.setNursingSchedulingNoon("中班");
                        }
                    }
                    //晚班
                    NursingSchedulingNight nursingSchedulingNight = nursingSchedulingNightService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
                    if(nursingSchedulingNight!=null){
                        NursingSchedulingNightItem nursingSchedulingNightItem = null;
                        if(userType==4){
                            nursingSchedulingNightItem = nursingSchedulingNightItemService.getListByNursingSchedulingNightCodeAndNurseCode(nursingSchedulingNight.getNursingSchedulingNightCode(),nurseCode);

                        }else if(userType==5){
                            nursingSchedulingNightItem = nursingSchedulingNightItemService.getListByNursingSchedulingNightCodeAndNurseAideCode(nursingSchedulingNight.getNursingSchedulingNightCode(),nurseAideCode);

                        }
                        if(nursingSchedulingNightItem!=null){
                            nursingSchedulingDTO1.setNursingSchedulingNight("晚班");
                        }
                    }
                }
            }
            nursingSchedulingPageInfo.setList(Collections.unmodifiableList(nursingSchedulingDTO1List));

            return success(nursingSchedulingPageInfo);
        } catch (Exception e) {
            insertError("/NursingScheduling/GetNurseAideWorking", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }
//
//    //护理提醒（后台）
//    @RequestMapping(value = "GetCareRemind", method = RequestMethod.POST)
//    public MyResult GetCareRemind(@RequestBody JSONObject jsonObject) {
//        try {
//            String timeStr = jsonObject.getString("timeStr");
//            String sign = jsonObject.getString("sign");
//            String staffCode = jsonObject.getString("staffCode");
//            int pageIndex = jsonObject.getInteger("pageIndex");
//            int pageSize = jsonObject.getInteger("pageSize");
//
//            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/NursingScheduling/GetCareRemind");
//            if (preCheck.getError() == 1) {
//                return failed(preCheck.getMessage());
//            }
//
//            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
//            String institutionCode = institutionStaffRel.getInstitutionCode();
//
//            NurseStaffRel nurseStaffRel = nurseStaffRelService.getByNurseStaffCode(staffCode);
//            String nurseCode =nurseStaffRel.getNurseCode();
//            NursingScheduling nursingScheduling = nursingSchedulingService.getByNowTime(institutionCode);
//            if(nursingScheduling==null){
//                return failed("今日无排班");
//            }
//            NursingSchedulingDTO1 nursingSchedulingDTO1 = mapper.map(nursingScheduling,NursingSchedulingDTO1.class);
//
//            if(!getWhetherScheduling(nursingSchedulingDTO1,"",nurseCode)){
//                return failed("今日无排班");
//            }
//            PageInfo<Checkin> checkinPageInfo = checkinService.getCheckinBeing(pageIndex, pageSize, institutionCode);
//            List<CheckinDTO> checkinDTOList = new ArrayList<>();
//            for (Checkin checkin : checkinPageInfo.getList()) {
//                CheckinDTO checkinDTO = mapper.map(checkin, CheckinDTO.class);
//                checkinDTOList.add(checkinDTO);
//            }
//            for (CheckinDTO checkinDTO : checkinDTOList) {
//                Elderly elderly = elderlyService.getByElderlyCode(checkinDTO.getElderlyCode());
//                checkinDTO.setBalance(elderly.getBalance());
//                User elderlyUser = userService.getByUserCode(elderly.getUserCode());
//                checkinDTO.setElderlyName(elderlyUser.getName());
//                CheckinSetmeal checkinSetmeal = checkinSetmealService.getByCheckinSetmealCode(checkinDTO.getCheckinSetmealCode());
//                checkinDTO.setCheckinSetmealName(checkinSetmeal.getName());
//                checkinDTO.setNursingLevelCode(checkinSetmeal.getNursingLevelCode());
//                checkinDTO.setNursingLevelName(checkinSetmeal.getNursingLevelName());
//                Berth berth = berthService.getByBerthCode(checkinDTO.getBerthCode());
//                if (berth == null) {
//                    return failed("床位不存在");
//                }
//                checkinDTO.setBerthNumber(berth.getNumber());
//                Room room = roomService.getByRoomCode(berth.getRoomCode());
//                if (room == null) {
//                    return failed("房间不存在");
//                }
//                String roomNumber = room.getNumber();
//                Building building = buildingService.getByBuildingCode(room.getBuildingCode());
//                if (building == null) {
//                    return failed("楼宇不存在");
//                }
//                String buildingName = building.getName();
//                checkinDTO.setLiveAddress(buildingName + "-" + roomNumber + "-" + checkinDTO.getBerthNumber());
//            }
//
//            checkinPageInfo.setList(Collections.unmodifiableList(checkinDTOList));
//
//            return success(checkinPageInfo);
//        } catch (Exception e) {
//            insertError("/NursingScheduling/GetCareRemind", jsonObject.toJSONString(), getExceptionMsg(e), "api");
//            return anomalous(getExceptionMsg(e));
//        }
//    }
//
//    //护理提醒（客户端）
//    @RequestMapping(value = "GetCareRemindApp", method = RequestMethod.POST)
//    public MyResult GetCareRemindApp(@RequestBody JSONObject jsonObject) {
//        try {
//            String timeStr = jsonObject.getString("timeStr");
//            String sign = jsonObject.getString("sign");
//            String userCode = jsonObject.getString("userCode");
//            int pageIndex = jsonObject.getInteger("pageIndex");
//            int pageSize = jsonObject.getInteger("pageSize");
//            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/NursingScheduling/GetCareRemind");
//            if (preCheck.getError() == 1) {
//                return failed(preCheck.getMessage());
//            }
//            User user = userService.getByUserCode(userCode);
//            if(user==null){
//                return failed("用户不存在");
//            }
//            int userType = user.getUserType();
//            String nurseCode ="";
//            String nurseAideCode ="";
//            String institutionCode = user.getInstitutionCode();
//            if(userType==4){
//                //Nurse nurse = nurseService.getByUserCode(userCode);
//                //nurseCode = nurse.getNurseCode();
//            }else if(userType==5){
//                NurseAide nurseAide = nurseAideService.getByUserCode(userCode);
//                nurseAideCode = nurseAide.getNurseAideCode();
//            }else {
//                return failed("用户类型错误");
//            }
//            NursingScheduling nursingScheduling = nursingSchedulingService.getByNowTime(institutionCode);
//            if(nursingScheduling==null){
//                return failed("今日无排班");
//            }
//            NursingSchedulingDTO1 nursingSchedulingDTO1 = mapper.map(nursingScheduling,NursingSchedulingDTO1.class);
//
//            if(!getWhetherScheduling(nursingSchedulingDTO1,nurseAideCode,nurseCode)){
//                return failed("今日无排班");
//            }
//            PageInfo<Checkin> checkinPageInfo = checkinService.getCheckinBeing(pageIndex, pageSize, institutionCode);
//            List<CheckinDTO> checkinDTOList = new ArrayList<>();
//            for (Checkin checkin : checkinPageInfo.getList()) {
//                CheckinDTO checkinDTO = mapper.map(checkin, CheckinDTO.class);
//                checkinDTOList.add(checkinDTO);
//            }
//            for (CheckinDTO checkinDTO : checkinDTOList) {
//                Elderly elderly = elderlyService.getByElderlyCode(checkinDTO.getElderlyCode());
//                checkinDTO.setBalance(elderly.getBalance());
//                User elderlyUser = userService.getByUserCode(elderly.getUserCode());
//                checkinDTO.setElderlyHeadImg(elderlyUser.getHeadImg());
//                checkinDTO.setElderlyName(elderlyUser.getName());
//                if(checkinDTO.getChargeType()==1){
//                    CheckinSetmeal checkinSetmeal = checkinSetmealService.getByCheckinSetmealCode(checkinDTO.getCheckinSetmealCode());
//                    checkinDTO.setCheckinSetmealName(checkinSetmeal.getName());
//                    checkinDTO.setNursingLevelCode(checkinSetmeal.getNursingLevelCode());
//                    checkinDTO.setNursingLevelName(checkinSetmeal.getNursingLevelName());
//                }else {
//                    checkinDTO.setCheckinSetmealName("自选配套餐");
//                    CheckinAutomatchDTO byCheckinAutomatchCode = checkinAutomatchService.getByCheckinAutomatchCode(checkinDTO.getCheckinAutomatchCode());
//                    checkinDTO.setNursingLevelCode(byCheckinAutomatchCode.getNursingLevelCode());
//                    checkinDTO.setNursingLevelName(byCheckinAutomatchCode.getNursingLevelName());
//                }
//                Berth berth = berthService.getByBerthCode(checkinDTO.getBerthCode());
//                if (berth == null) {
//                    return failed("床位不存在");
//                }
//                checkinDTO.setBerthNumber(berth.getNumber());
//                Room room = roomService.getByRoomCode(berth.getRoomCode());
//                if (room == null) {
//                    return failed("房间不存在");
//                }
//                String roomNumber = room.getNumber();
//                Building building = buildingService.getByBuildingCode(room.getBuildingCode());
//                if (building == null) {
//                    return failed("楼宇不存在");
//                }
//                String buildingName = building.getName();
//                checkinDTO.setLiveAddress(buildingName + "-" + roomNumber + "-" + checkinDTO.getBerthNumber());
//
//            }
//
//            checkinPageInfo.setList(Collections.unmodifiableList(checkinDTOList));
//
//            return success(checkinPageInfo);
//        } catch (Exception e) {
//            insertError("/NursingScheduling/GetCareRemind", jsonObject.toJSONString(), getExceptionMsg(e), "api");
//            return anomalous(getExceptionMsg(e));
//        }
//    }

    //查看今日有无排班
    private boolean getWhetherScheduling(NursingSchedulingDTO1 nursingSchedulingDTO1,String nurseAideCode,String nurseCode){
        int type = nursingSchedulingDTO1.getType();
        if(type==2) {
            NursingSchedulingDaytime nursingSchedulingDaytime = nursingSchedulingDaytimeService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
            if(nursingSchedulingDaytime!=null){
                NursingSchedulingDaytimeItem nursingSchedulingDaytimeItem = null;
                if(nurseAideCode.equals("")){
                    nursingSchedulingDaytimeItem = nursingSchedulingDaytimeItemService.getByNursingSchedulingDayTimeCodeAndNurseCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode(),nurseCode);
                }else{
                    nursingSchedulingDaytimeItem = nursingSchedulingDaytimeItemService.getByNursingSchedulingDayTimeCodeAndNurseAideCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode(),nurseAideCode);
                }
                if(nursingSchedulingDaytimeItem!=null)return true;
            }
            //晚班
            NursingSchedulingNighttime nursingSchedulingNighttime = nursingSchedulingNighttimeService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
            if(nursingSchedulingNighttime!=null){
                NursingSchedulingNighttimeItem nursingSchedulingNighttimeItem = null;
                if(nurseAideCode.equals("")){
                    nursingSchedulingNighttimeItem = nursingSchedulingNighttimeItemService.getByNursingSchedulingNightTimeCodeAndNurseCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode(),nurseCode);
                }else{
                    nursingSchedulingNighttimeItem = nursingSchedulingNighttimeItemService.getByNursingSchedulingNightTimeCodeAndNurseAideCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode(),nurseAideCode);
                }
                if(nursingSchedulingNighttimeItem!=null)return true;
            }
        }
        if(type==3){
            //早班
            NursingSchedulingMorning nursingSchedulingMorning = nursingSchedulingMorningService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
            if(nursingSchedulingMorning!=null){
                NursingSchedulingMorningItem nursingSchedulingMorningItem = null;
                if(nurseAideCode.equals("")){
                    nursingSchedulingMorningItem = nursingSchedulingMorningItemService.getByNursingSchedulingMorningCodeAndNurseCode(nursingSchedulingMorning.getNursingSchedulingMorningCode(),nurseCode);
                }else{
                    nursingSchedulingMorningItem = nursingSchedulingMorningItemService.getByNursingSchedulingMorningCodeAndNurseAideCode(nursingSchedulingMorning.getNursingSchedulingMorningCode(),nurseAideCode);
                }
                if(nursingSchedulingMorningItem!=null)return true;
            }
            //中班
            NursingSchedulingNoon nursingSchedulingNoon = nursingSchedulingNoonService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
            if(nursingSchedulingNoon!=null){
                NursingSchedulingNoonItem nursingSchedulingNoonItem = null;
                if(nurseAideCode.equals("")){
                    nursingSchedulingNoonItem = nursingSchedulingNoonItemService.getByNursingSchedulingNoonCodeAndNurseCode(nursingSchedulingNoon.getNursingSchedulingNoonCode(),nurseCode);
                }else{
                    nursingSchedulingNoonItem = nursingSchedulingNoonItemService.getByNursingSchedulingNoonCodeAndNurseAideCode(nursingSchedulingNoon.getNursingSchedulingNoonCode(),nurseAideCode);
                }
                if(nursingSchedulingNoonItem!=null)return true;
            }
            //晚班
            NursingSchedulingNight nursingSchedulingNight = nursingSchedulingNightService.getByNursingSchedulingCode(nursingSchedulingDTO1.getNursingSchedulingCode());
            if(nursingSchedulingNight!=null){
                NursingSchedulingNightItem nursingSchedulingNightItem = null;
                if(nurseAideCode.equals("")){
                    nursingSchedulingNightItem = nursingSchedulingNightItemService.getListByNursingSchedulingNightCodeAndNurseCode(nursingSchedulingNight.getNursingSchedulingNightCode(),nurseCode);

                }else{
                    nursingSchedulingNightItem = nursingSchedulingNightItemService.getListByNursingSchedulingNightCodeAndNurseAideCode(nursingSchedulingNight.getNursingSchedulingNightCode(),nurseAideCode);
                }
                if(nursingSchedulingNightItem!=null)return true;
            }
        }
        return false;
    }

}
